/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
define("pages/scqk/scqk_form", [
        "../../plugins/easyui/jquery-easyui-1.5.1/jquery.easyui.min",
        "../../plugins/easyui/jquery-easyui-1.5.1/easyloader",
        '../../pages/scqk/scqk_form.html.js',
        "../../common/componts",
        "../../common/tools"
    ],
    function(require){
        var layerIndex;
        var _callback;
        var formTemplate = require("./scqk_form.html");
        var componts = require("../../common/componts");

        /**
         * 获取表单内容
         * @param initData
         * @returns {*}
         */
        var getFormHtml = function(initData){
            if (!initData) {
                initData = {};
                initData.rybh='';
                initData.jsbh='';
            }
            var htmlCompile = template.compile(formTemplate);
            var username = CURRENT_USER.username;//获取当前办理人
            var html = htmlCompile({
                tbr:username,
                tbrq:currentDate(),
                data:initData
            });
            return html;
        };
        /**
         * 动态渲染
         */
        var parseEasyUIElement = function(){
            $.parser.parse($(".awd-form-panel"));
        };

        var _show = function(initData,callback){
            _callback = callback;
            var componts = require("../../common/componts");
            layerIndex = layer.open({
                type: 1,
                title: "数据录入",
                skin: 'layui-layer-lan', //加上边框
                closeBtn: 1, //不显示关闭按钮
                shadeClose: true, //开启遮罩关闭
                content: getFormHtml(initData),
                area:['100%','100%'],
                success: function(layero, index){
                    console.log(layero, index);
                }
            });
            layer.full(layerIndex);
            setTimeout(function(){
                //初始化下拉框
                componts.initSelectPage(undefined);
                componts.initSelect(undefined,undefined,undefined);
                parseEasyUIElement(); //动态渲染
            },100);
        };

        var save = function(){
            var url = "/scqk/save";
            var $form = $(".awd-form");
            if (!$form.form('validate')) {
                $.messager.alert("提示", '必填项不能为空！');
                return;
            }
            var jbxx = {};
            var list=new Array();
            var jbxxcount=$("#jtbody").find("tr").length;
            for(var i=1;i<=jbxxcount;i++){
                var tbr=$("#tbr").val();
                var tbrq=$("#tbrq").val();
                var ywlcid=$("#ywlcid").val();
                var taskid=$("#taskid").val();
                var jsbh=$("#jsbh").val()
                var rq=$("#rq").val()
                var ldxm=$("#ldxm").val()
                var ldzw=$("#ldzw").val()
                var xm= $("#xm"+i).val();
                var zw = $("#zw"+i).val();
                var szdw = $("#szdw"+i).val();
                var scnr = $("#scnr").val();
                var yjjy = $("#yjjy").val();
                jbxx = {"tbr":tbr,"tbrq":tbrq,"ywlcid":ywlcid,"taskid":taskid,"jsbh":jsbh,"rq":rq,"ldxm":ldxm,"ldzw":ldzw,"xm":xm,"zw":zw,"szdw":szdw,"scnr":scnr,"yjjy":yjjy};
                list.push(jbxx);
            }
            var data =JSON.stringify(list);
            console.log('form data -> ',data);
            var loadingIndex = layer.load();
            $.ajax({
                type : "POST",  //提交方式
                url : url,
                data : {jsonList:data},//数据，这里使用的是Json格式进行传输
                success : function(result) {
                    layer.closeAll();
                    if (result.success) {
                        _callback && _callback();
                        $.messager.alert("确定","保存成功","info");
                    }else{
                        $.messager.alert("确定","保存失败","warn");
                    }
                },
                complete:function(){
                    layer.close(loadingIndex);
                }
            });
        };

        var bindAction = function(){
            //保存按钮事件
            $(document).on('click','.save-button',function () {
                save();
            });
            //点击关闭按钮
            $(document).on('click','.close-button',function(){
                layer.close(layerIndex);
                layerIndex = null;
            });

            var serialNum = 4;
            var cjryxxHeight= parseInt(43);
            $(document).on('click','.cjryxx .add-row',function(){
                var trId = 'record_' + serialNum;
                var trHtml = '<tr id="'+trId+'" class="'+trId+'">'+
                    '   <td><input id="xm'+serialNum+'" type="text" name="xm" class="form-control"></td>'+
                    '   <td><input id="zw'+serialNum+'" style="width: 100%" type="text" name="zw" code="ZW" class="form-control awdSelect"></td>'+
                    '   <td><input id="szdw'+serialNum+'" type="text" name="szdw" class="form-control"></td>'+
                    '   <td><a href="javascript:void(0)" class="add-row" data-id="'+trId+'" title="增加">+</a>&nbsp;<a href="javascript:void(0)" class="del-row"  data-id="'+trId+'" title="删除">-</a></td>'+
                    '</tr>';
                $(".cjryxx table").find("tr:last").parent().append(trHtml);
                componts.initSelect();
                // $.parser.parse($("#"+trId));
                // componts.initSelect(undefined,undefined,undefined);

                $('.cjryxx').attr('style','height:'+($('.cjryxx').height()+cjryxxHeight)+'px;')
                serialNum++;
            });


            $(document).on('click','.cjryxx .del-row',function(){
                var trId = $(this).data("id");
                $("#"+trId).remove();
                $('.cjryxx').attr('style','height:'+($('.cjryxx').height()-cjryxxHeight)+'px;')
            });

            //修正layer 最大化，解决办法..
            $(window).resize(function(){
                layerIndex && layer.full(layerIndex);
                parseEasyUIElement();//TODO::后续优化
            })
        };

        (function(){
            componts.initSelectPage(undefined);
            componts.initSelect(undefined,undefined,undefined);
            bindAction();
        })()

        return{
            show:_show
        };
    });